package com.see.services.impl;

import java.util.Iterator;
import java.util.List;

import com.see.domain.dao.DataNucleusUtil;
import com.see.domain.model.StaticData;
import com.see.domain.model.Symbol;
import com.see.network.api.HtmlReader;
import com.see.network.impl.HtmlReaderImpl;
import com.see.services.api.StaticDataUpdator;

public class StaticDataUpdatorImpl implements StaticDataUpdator {

	// http://finance.yahoo.com/q/pr?s=BHP.AX+Profile
	private final static String BASE_URL = "http://finance.yahoo.com/q/pr?s=";
	private final static String PROFILE_POSTFIX = "+Profile";

	private final static String SECTOR_ANCHOR = ">Sector:<";
	private final static String INDUSTRY_ANCHOR = ">Industry:<";
	private final static String COMPANY_NAME_ANCHOR = ">Search for share prices<";
	
	private final static String FIELD_START_TAG = ".html\">";
	private final static String FIELD_END_TAG = "</a>";
	
	private final static String COMPANY_NAME_START_TAG = "<b>";
	private final static String COMPANY_NAME_END_TAG = "</b>";

	private final static String NO_PROFILE_FOUND_PATTERN = "There is no Profile data available for";

	private HtmlReader htmlReader;
	private DataNucleusUtil dataNucleusUtil;

	public StaticDataUpdatorImpl() {
		htmlReader = new HtmlReaderImpl();

		dataNucleusUtil = new DataNucleusUtil();
	}

	@Override
	public Symbol fetchSectorAndIndustryBySymbol(String inputSymbol) {
		Symbol symbol = new Symbol();

		String targetUrl = getTargetProfileURL(inputSymbol);
		String content = htmlReader.readHtml(targetUrl, false);

		//if (!content.contains(NO_PROFILE_FOUND_PATTERN)) {
			String sectorString = extractTargetValue(SECTOR_ANCHOR, content);
			String industryString = extractTargetValue(INDUSTRY_ANCHOR, content);
			String companyNameString = extractTargetValue(COMPANY_NAME_ANCHOR, content, COMPANY_NAME_START_TAG, COMPANY_NAME_END_TAG);

			symbol.setSector(sectorString);
			symbol.setIndustry(industryString);
			symbol.setCompanyName(companyNameString);
		//}
		symbol.setTicker(inputSymbol);
		return symbol;
	}
	
	private String extractTargetValue(String anchor, String content, String startTag, String endTag) {
		int targetOffset = content.indexOf(anchor) + anchor.length();
		String trimContent = content.substring(targetOffset);

		int startOffset = trimContent.indexOf(FIELD_START_TAG)
				+ FIELD_START_TAG.length();
		int endOffset = trimContent.indexOf(FIELD_END_TAG);
		String targetValue = "";
		try {
			targetValue = trimContent.substring(startOffset, endOffset).trim();
		} catch (Exception e) {
			targetValue = "N/A";
			// System.out.println("value is: " + targetValue);
		}

		return targetValue;
	}

	private String extractTargetValue(String anchor, String content) {
		return extractTargetValue(anchor, content, FIELD_START_TAG, FIELD_END_TAG);
	}

	private String getTargetProfileURL(String symbol) {
		return BASE_URL + symbol + PROFILE_POSTFIX;
	}

	public static void main(String[] args) {
		StaticDataUpdator updator = new StaticDataUpdatorImpl();
		updator.fetchSectorAndIndustryBySymbol("BHP.AX");
	}

	@Override
	public void updateSectorAndIndustryForAllRecords(
			List<StaticData> actualResultCollection) {
		Iterator iterator = actualResultCollection.iterator();

		while (iterator.hasNext()) {
			StaticData staticData = (StaticData) iterator.next();
			if ("".equals(staticData.getSector()) || staticData.getSector() == null
					|| "".equals(staticData.getIndustry()) || staticData.getIndustry() == null
					|| "".equals(staticData.getCompanyName()) || staticData.getCompanyName() == null) {
				Symbol symbolObject = fetchSectorAndIndustryBySymbol(staticData
						.getSymbol());

				dataNucleusUtil.updateSectorAndIndustryBySymbol(
						symbolObject.getTicker(), symbolObject.getSector(),
						symbolObject.getIndustry());

			}

		}

	}

}
